package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class Delete implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("delete");
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入书名");
        String name = scan.next();
        int index = -1;
        for (int i = 0; i < bookList.getUsedsize(); i++) {
            Book delbook = bookList.getBook(i);
            if (delbook.getName().equals(name)) {
                index = i;
                break;
            }
        }

        if(index!=-1){
            int currentsize = bookList.getUsedsize();
            for (int k = index; k < currentsize - 1; k++) {
                Book nextbook = bookList.getBook(k + 1);
                bookList.setBook(k, nextbook);

            }
            bookList.setUsedsize(currentsize - 1);
            System.out.println("删除成功");
        }
        else{
            System.out.println("没有这本书");

        }



    }
}
